package edu.szcu.vitaguard.dao;

import edu.szcu.vitaguard.entity.PrescriptionItem;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 处方项DAO接口
 */
@Mapper
public interface PrescriptionItemDao {

    /**
     * 插入处方项
     * @param prescriptionItem 处方项对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO prescription_item(prescription_id, medicine_id, dosage, frequency, " +
            "duration, quantity, instructions, create_time, update_time) " +
            "VALUES(#{prescriptionId}, #{medicineId}, #{dosage}, #{frequency}, " +
            "#{duration}, #{quantity}, #{instructions}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "itemId")
    int insert(PrescriptionItem prescriptionItem);

    /**
     * 批量插入处方项
     * @param prescriptionItems 处方项列表
     * @return 影响的行数
     */
    @Insert("<script>" +
            "INSERT INTO prescription_item(prescription_id, medicine_id, dosage, frequency, " +
            "duration, quantity, instructions, create_time, update_time) VALUES " +
            "<foreach collection='list' item='item' separator=','>" +
            "(#{item.prescriptionId}, #{item.medicineId}, #{item.dosage}, #{item.frequency}, " +
            "#{item.duration}, #{item.quantity}, #{item.instructions}, #{item.createTime}, #{item.updateTime})" +
            "</foreach>" +
            "</script>")
    int batchInsert(List<PrescriptionItem> prescriptionItems);

    /**
     * 根据ID查询处方项
     * @param itemId 处方项ID
     * @return 处方项对象
     */
    @Select("SELECT pi.*, p.prescription_date, p.diagnosis, p.instructions as prescription_instructions, " +
            "m.medicine_name, m.medicine_type, m.specification, m.unit_price " +
            "FROM prescription_item pi " +
            "LEFT JOIN prescription p ON pi.prescription_id = p.prescription_id " +
            "LEFT JOIN medicine m ON pi.medicine_id = m.medicine_id " +
            "WHERE pi.item_id = #{itemId}")
    @Results({
        @Result(property = "itemId", column = "item_id"),
        @Result(property = "prescriptionId", column = "prescription_id"),
        @Result(property = "medicineId", column = "medicine_id"),
        @Result(property = "dosage", column = "dosage"),
        @Result(property = "frequency", column = "frequency"),
        @Result(property = "duration", column = "duration"),
        @Result(property = "quantity", column = "quantity"),
        @Result(property = "instructions", column = "instructions"),
        @Result(property = "prescription", column = "prescription_id", javaType = edu.szcu.vitaguard.entity.Prescription.class,
                one = @One(select = "edu.szcu.vitaguard.dao.PrescriptionDao.selectById")),
        @Result(property = "medicine", column = "medicine_id", javaType = edu.szcu.vitaguard.entity.Medicine.class,
                one = @One(select = "edu.szcu.vitaguard.dao.MedicineDao.selectById"))
    })
    PrescriptionItem selectById(Integer itemId);

    /**
     * 根据处方ID查询处方项
     * @param prescriptionId 处方ID
     * @return 处方项列表
     */
    @Select("SELECT pi.*, p.prescription_date, p.diagnosis, p.instructions as prescription_instructions, " +
            "m.medicine_name, m.medicine_type, m.specification, m.unit_price " +
            "FROM prescription_item pi " +
            "LEFT JOIN prescription p ON pi.prescription_id = p.prescription_id " +
            "LEFT JOIN medicine m ON pi.medicine_id = m.medicine_id " +
            "WHERE pi.prescription_id = #{prescriptionId}")
    @Results({
        @Result(property = "itemId", column = "item_id"),
        @Result(property = "prescriptionId", column = "prescription_id"),
        @Result(property = "medicineId", column = "medicine_id"),
        @Result(property = "dosage", column = "dosage"),
        @Result(property = "frequency", column = "frequency"),
        @Result(property = "duration", column = "duration"),
        @Result(property = "quantity", column = "quantity"),
        @Result(property = "instructions", column = "instructions"),
        @Result(property = "prescription", column = "prescription_id", javaType = edu.szcu.vitaguard.entity.Prescription.class,
                one = @One(select = "edu.szcu.vitaguard.dao.PrescriptionDao.selectById")),
        @Result(property = "medicine", column = "medicine_id", javaType = edu.szcu.vitaguard.entity.Medicine.class,
                one = @One(select = "edu.szcu.vitaguard.dao.MedicineDao.selectById"))
    })
    List<PrescriptionItem> selectByPrescriptionId(Integer prescriptionId);

    /**
     * 根据药品ID查询处方项
     * @param medicineId 药品ID
     * @return 处方项列表
     */
    @Select("SELECT pi.*, p.prescription_date, p.diagnosis, p.instructions as prescription_instructions, " +
            "m.medicine_name, m.medicine_type, m.specification, m.unit_price " +
            "FROM prescription_item pi " +
            "LEFT JOIN prescription p ON pi.prescription_id = p.prescription_id " +
            "LEFT JOIN medicine m ON pi.medicine_id = m.medicine_id " +
            "WHERE pi.medicine_id = #{medicineId}")
    @Results({
        @Result(property = "itemId", column = "item_id"),
        @Result(property = "prescriptionId", column = "prescription_id"),
        @Result(property = "medicineId", column = "medicine_id"),
        @Result(property = "dosage", column = "dosage"),
        @Result(property = "frequency", column = "frequency"),
        @Result(property = "duration", column = "duration"),
        @Result(property = "quantity", column = "quantity"),
        @Result(property = "instructions", column = "instructions"),
        @Result(property = "prescription", column = "prescription_id", javaType = edu.szcu.vitaguard.entity.Prescription.class,
                one = @One(select = "edu.szcu.vitaguard.dao.PrescriptionDao.selectById")),
        @Result(property = "medicine", column = "medicine_id", javaType = edu.szcu.vitaguard.entity.Medicine.class,
                one = @One(select = "edu.szcu.vitaguard.dao.MedicineDao.selectById"))
    })
    List<PrescriptionItem> selectByMedicineId(Integer medicineId);

    /**
     * 更新处方项
     * @param prescriptionItem 处方项对象
     * @return 影响的行数
     */
    @Update("UPDATE prescription_item SET prescription_id = #{prescriptionId}, medicine_id = #{medicineId}, " +
            "dosage = #{dosage}, frequency = #{frequency}, duration = #{duration}, " +
            "quantity = #{quantity}, instructions = #{instructions}, update_time = #{updateTime} " +
            "WHERE item_id = #{itemId}")
    int update(PrescriptionItem prescriptionItem);

    /**
     * 删除处方项
     * @param itemId 处方项ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM prescription_item WHERE item_id = #{itemId}")
    int deleteById(Integer itemId);

    /**
     * 根据处方ID删除处方项
     * @param prescriptionId 处方ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM prescription_item WHERE prescription_id = #{prescriptionId}")
    int deleteByPrescriptionId(Integer prescriptionId);
}
